//
//  MineViewController.m
//  mosad_hw_fin
//
//  Created by student9 on 2020/12/24.
//  Copyright © 2020 student9. All rights reserved.
//

#import "MineViewController.h"

@interface MineViewController ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    __weak typeof(self) weakSelf = self;
    headimageModel *current_head_image = [headimageModel shareInstance];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:230/255.0 green:230/255.0  blue:250/255.0 alpha:1];
    self.title = @"我的";
    
    UIView *backgroundMine = [[UIView alloc]  initWithFrame:CGRectMake(0, 0, weakSelf.view.frame.size.width, weakSelf.view.frame.size.height / 2.4)];
    backgroundMine.backgroundColor = [UIColor colorWithPatternImage:[[UIImage imageNamed:@"backgroundMine.png" ]TransformtoSize:CGSizeMake(weakSelf.view.frame.size.width, weakSelf.view.frame.size.height / 2.4)]] ;
    [self.view addSubview:backgroundMine];
    
    UIView *borderMine = [[UIView alloc] initWithFrame: CGRectMake(0, 0, weakSelf.view.frame.size.width - 60, weakSelf.view.frame.size.height - 160)];
    borderMine.backgroundColor = [UIColor whiteColor];
    UIBezierPath * maskPath_borderMine = [UIBezierPath bezierPathWithRoundedRect:borderMine.bounds cornerRadius:30];
    CAShapeLayer * maskLayer_borderMine = [[CAShapeLayer alloc] init];
    maskLayer_borderMine.frame = borderMine.bounds;
    maskLayer_borderMine.path = maskPath_borderMine.CGPath;
    borderMine.layer.mask = maskLayer_borderMine;
    [self.view addSubview:borderMine];
    [borderMine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(weakSelf.view.frame.size.width - 60, weakSelf.view.frame.size.height - 160));
        make.centerX.equalTo(weakSelf.view.mas_centerX);
        make.centerY.equalTo(weakSelf.view.mas_centerY);
    }];
    
    UIButton *head_image = [[UIButton alloc ] initWithFrame:CGRectMake(0, 0, 100, 100)];
    head_image.backgroundColor = [UIColor colorWithPatternImage:[[UIImage imageNamed:current_head_image.headimage_url] TransformtoSize:CGSizeMake(100, 100)]];
    UIBezierPath * maskPath_head = [UIBezierPath bezierPathWithRoundedRect:head_image.bounds cornerRadius:head_image.frame.size.width/2];
    CAShapeLayer * maskLayer_head = [[CAShapeLayer alloc] init];
    maskLayer_head.frame = head_image.bounds;
    maskLayer_head.path = maskPath_head.CGPath;
    head_image.layer.mask = maskLayer_head;
    [head_image addTarget:self
                  action:@selector(NotLoginButtonView:)
        forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:head_image];
    [head_image mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(borderMine.mas_top).offset(50);
        make.size.mas_equalTo(CGSizeMake(100, 100));
        make.centerX.equalTo(weakSelf.view.mas_centerX);
    }];
    
    self.username = [UILabel new];
    self.username.frame = CGRectMake(0, 0, weakSelf.view.frame.size.width - 100, 50);
    self.username.text = current_head_image.name;
    self.username.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.username];
    [self.username mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(head_image.mas_bottom).offset(5);
        make.size.mas_equalTo(CGSizeMake(weakSelf.view.frame.size.width - 100, 50));
        make.centerX.equalTo(weakSelf.view.mas_centerX);
    }];
    
    _outbtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 30) ];
    _outbtn.backgroundColor = [UIColor colorWithRed:123/255.0 green:104/255.0 blue:238/255.0 alpha:0.7];
    [_outbtn setTitle:@"退出登录" forState:UIControlStateNormal];
    [_outbtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _outbtn.layer.cornerRadius = 10;
    if ([current_head_image.name isEqualToString:@"未登录"] == NO) {
        _outbtn.layer.hidden = NO;
    } else {
        _outbtn.layer.hidden = YES;
    }
    [_outbtn addTarget:self
                action:@selector(outLogin:)
      forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:_outbtn];
//    [_outbtn mas_remakeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.username.mas_bottom).offset(0);
//        make.size.mas_equalTo(CGSizeMake(100, 30));
//        make.centerX.equalTo(weakSelf.view.mas_centerX);
//    }];
    
    
    
    UITableView *operTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, weakSelf.view.frame.size.width - 100, 240)];
    operTableView.delegate = weakSelf;
    operTableView.dataSource = weakSelf;
    operTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    operTableView.backgroundColor = [UIColor whiteColor];
    operTableView.scrollEnabled = NO;
    [self.view addSubview:operTableView];
    for (NSDictionary *dict in [self arrayDataSource]) {
        operatorTableViewModel *model = [operatorTableViewModel new];
        model.image_url = dict[@"image_url"];
        model.title = dict[@"title"];
        [self.dataSource addObject:model];
    }
    [operTableView reloadData];
    [operTableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.username.mas_bottom).offset(30);
        make.size.mas_equalTo(CGSizeMake(weakSelf.view.frame.size.width - 100, 240));
        make.centerX.equalTo(weakSelf.view.mas_centerX);
    }];
}

#pragma mark - TableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataSource.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ident = @"Cell";
    OperatorTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ident];
    if (!cell) {
        cell = [[OperatorTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ident];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    operatorTableViewModel *model = self.dataSource[indexPath.section];
    [cell setModel:model];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        HoverViewController *hoverVC = [[HoverViewController alloc] init];
        [self.navigationController pushViewController:hoverVC animated:YES];
    } else if (indexPath.section == 2) {
        HelpViewController *helpVC = [[HelpViewController alloc] init];
        [self.navigationController pushViewController:helpVC animated:YES];
    } else if (indexPath.section == 0) {
        MyTypePageController * MytypeVC = [[MyTypePageController alloc] init];
        [self.navigationController pushViewController:MytypeVC animated:YES];
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

-(NSArray *)arrayDataSource {
    NSMutableArray *jsonResult = [[NSMutableArray alloc] init];
    NSMutableDictionary *myunijson = [[NSMutableDictionary alloc] init];
    myunijson[@"image_url"] = @"myuni.png";
    myunijson[@"title"] = @"我的生活";
    [jsonResult addObject:myunijson];
    NSMutableDictionary *hoverjson = [[NSMutableDictionary alloc] init];
    hoverjson[@"image_url"] = @"hover.png";
    hoverjson[@"title"] = @"点滴成就";
    [jsonResult addObject:hoverjson];
    NSMutableDictionary *helpjson = [[NSMutableDictionary alloc] init];
    helpjson[@"image_url"] = @"help.png";
    helpjson[@"title"] = @"帮助中心";
    [jsonResult addObject:helpjson];
    NSMutableDictionary *extendjson = [[NSMutableDictionary alloc] init];
    extendjson[@"image_url"] = @"extend.png";
    extendjson[@"title"] = @"扩展功能";
    [jsonResult addObject:extendjson];
    return jsonResult;
}

-(NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray new];
    }
    return _dataSource;
}

- (void)NotLoginButtonView:(UIButton *)buttonView {
    headimageModel *current_head_image = [headimageModel shareInstance];
    if ([current_head_image.name isEqualToString:@"未登录"]) {
        LoginViewController *testVC = [[LoginViewController alloc] init];
        testVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:testVC animated:NO];
    }
}

- (void) viewDidAppear:(BOOL)animated {
    [self viewDidLoad];
}

- (void)outLogin:(UIButton *)buttonView {
    headimageModel *current_head_image = [headimageModel shareInstance];
    if ([current_head_image.name isEqualToString:@"未登录"] == NO) {
        current_head_image.headimage_url = @"notlogin";
        current_head_image.name = @"未登录";
        current_head_image.ID = @"notlogin";
        [self viewDidLoad];
    }
}

@end
